jQuery(function() {
    var $ = jQuery,    // just in case. Make sure it's not an other libaray.
        $wrap = $('#uploader'),
        $queue_last = $wrap.find('.webuploader-upload-btn'),
        // WebUploader实例
        uploader;

    if ( !WebUploader.Uploader.support() ) {
        alert( 'Web Uploader 不支持您的浏览器！如果你使用的是IE浏览器，请尝试升级 flash 播放器');
        throw new Error( 'WebUploader does not support the browser you are using.' );
    }

    // 实例化
    uploader = WebUploader.create({
        pick: {
            id: '#filePicker',
            label: '点击选择图片'
        },
        paste: document.body,
        accept: {
            title: 'Images',
            extensions: 'gif,jpg,jpeg,png',
            mimeTypes: 'image/*'
        },
        // swf文件路径
        swf: '/plugins/webuploader/Uploader.swf',

        auto:true,
        server: '/core/uploader/image',
        fileNumLimit: 5,
        fileSizeLimit: 2 * 1024 * 1024,    // 200 M
        fileSingleSizeLimit: 1 * 1024 * 1024    // 50 M
    });

    // 上传完成后
    uploader.on('uploadAccept', function (file, response) {
        if(response.id > 0){
            var src = response.result;
            var iname = new Date().getTime() + ''+ Math.ceil(Math.random()*100);
            $queue_last.before('<li><img src="/'+ src +'"><i class="btn-i"></i> <input type="hidden" name="images['+iname+']" value="'+src+'"></li>');
        }
    });

    $wrap.on('click', '.btn-i', function () {
        $(this).parent('li').remove();
    });
});